package com.example.hospital.patient.wx.api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.example.hospital.patient.wx.api.common.PageUtils;
import com.example.hospital.patient.wx.api.common.R;
import com.example.hospital.patient.wx.api.config.tencent.TrtcUtil;
import com.example.hospital.patient.wx.api.controller.form.*;
import com.example.hospital.patient.wx.api.db.pojo.VideoDiagnoseEntity;
import com.example.hospital.patient.wx.api.service.VideoDiagnoseService;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/video_diagnose")
public class VideoDiagnoseController {
    @Resource
    private VideoDiagnoseService videoDiagnoseService;
    @Value("${tencent.trtc.appId}")
    private String appId;
    @Resource
    private TrtcUtil trtcUtil;

    @PostMapping("searchOnlineDoctorList")
    @SaCheckLogin
    public R searchOnlineDoctorList(@RequestBody @Valid SearchOnlineDoctorListForm form) {
        ArrayList<HashMap> list = videoDiagnoseService.searchOnlineDoctorList(form.getSubName(), form.getJob());
        System.out.println(list);
        return R.ok().put("result", list);
    }

    @PostMapping("createVideoDiagnose")
    @SaCheckLogin
    public R createVideoDiagnose(@RequestBody @Valid CreateVideoDiagnoseForm form) {
        int userId = StpUtil.getLoginIdAsInt();
        VideoDiagnoseEntity entity = BeanUtil.toBean(form, VideoDiagnoseEntity.class);
        HashMap result = videoDiagnoseService.createVideoDiagnose(userId, entity);
        return R.ok().put("result", result);
    }

    @PostMapping("/transactionCallback")
    public R transactionCallback(@RequestBody @Valid TransactionCallbackForm form) {
        Map param = BeanUtil.beanToMap(form);
        param.put("paymentStatus", 2);
        videoDiagnoseService.updatePayment(param);
        return R.ok().put("result", "支付成功");
    }

    @PostMapping("/searchImageByVideoDiagnoseId")
    @SaCheckLogin
    public R searchImageByVideoDiagnoseId(@RequestBody @Valid SearchImageByVideoDiagnoseIdForm form) {
        ArrayList<HashMap> list = videoDiagnoseService.searchImageByVideoDiagnoseId(form.getVideoDiagnoseId());
        return R.ok().put("result", list);
    }

    @PostMapping("/uploadImage")
    @SaCheckLogin
    public R uploadImage(@Param("file") MultipartFile file, @Param("orderId") Integer videoDiagnoseId) {
        String filename = videoDiagnoseService.uploadImage(file, videoDiagnoseId);
        return R.ok().put("filename", filename);
    }

    @PostMapping("/deleteImage")
    @SaCheckLogin
    public R deleteImage(@RequestBody @Valid DeleteVideoDiagnoseImageForm form) {
        Map param = BeanUtil.beanToMap(form);
        videoDiagnoseService.deleteImage(param);
        return R.ok();
    }

    @GetMapping("/searchUserSig")
    @SaCheckLogin
    public R searchUserSig() {
        int userId = StpUtil.getLoginIdAsInt();
        String userSig = trtcUtil.genUserSig(userId + "");
        Map result = new HashMap<>();
        result.put("userSig", userSig);
        result.put("userId", userId);
        result.put("appId", appId);
        return R.ok().put("result", result);
    }

    @PostMapping("/searchRoomId")
    @SaCheckLogin
    public R searchRoomId(@RequestBody @Valid SearchRoomIdForm form) {
        String roomId = videoDiagnoseService.searchRoomId(form.getDoctorId());
        return R.ok().put("result", roomId);
    }

    @PostMapping("/searchByPage")
    @SaCheckLogin
    public R searchByPage(@RequestBody @Valid SearchVideoDiagnoseByPageForm form) {
        int userId = StpUtil.getLoginIdAsInt();
        form.setUserId(userId);
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        Map param = BeanUtil.beanToMap(form);
        param.put("start", start);
        PageUtils pageUtils = videoDiagnoseService.searchByPage(param);
        return R.ok().put("result", pageUtils);
    }
    @GetMapping("/checkFace")
    @SaCheckLogin
    public R checkFace(){
        int id = StpUtil.getLoginIdAsInt();
        String result = videoDiagnoseService.checkFaceCondition(id);
        return R.ok().put("result", result);
    }

    @PutMapping("/deleteById")
    @SaCheckLogin
    public R deleteById(@RequestBody @Param("data") Map data){
        Integer id = (Integer) data.get("id");
        videoDiagnoseService.deleteById(id);
        return R.ok().put("result", "删除成功");
    }
}

